Completed
Push — master ( bebc18...c39f44 )
by Sander
01:09
created

angular.controller(ꞌSetupCtrlꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 118

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
nop 5
dl 0
loc 118
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
(function () {
26
    'use strict';
27
28
    /**
29
     * @ngdoc function
30
     * @name passmanApp.controller:MainCtrl
31
     * @description
32
     * # MainCtrl
33
     * Controller of the passmanApp
34
     */
35
    angular.module('passmanExtension')
36
        .controller('SetupCtrl', ['$scope', '$timeout', '$location', '$rootScope', 'StepsService', function ($scope, $timeout, $location, $rootScope, StepsService) {
37
            $scope.settings = {
38
                nextcloud_host: '',
39
                nextcloud_username: '',
40
                nextcloud_password: '',
41
                ignoreProtocol: true,
42
                ignoreSubdomain: true,
43
                ignorePath: true,
44
                generatedPasswordLength: 12,
45
                remember_password: true,
46
                remember_vault_password: true,
47
                vault_password: '',
48
                refreshTime: 60,
49
                default_vault: {},
50
                master_password: '',
51
                disableAutoFill: false,
52
                disablePasswordPicker: false,
53
                disable_browser_autofill: true,
54
                debug: false
55
            };
56
            $scope.vaults = [];
57
58
            $scope.gogo = function (to) {
59
                StepsService.steps().goTo(to);
60
            };
61
62
            $scope.check = {
63
                server: function (callback) {
64
                    PAPI.host = $scope.settings.nextcloud_host;
65
                    PAPI.username = $scope.settings.nextcloud_username;
66
                    PAPI.password = $scope.settings.nextcloud_password;
67
                    PAPI.getVaults(function (vaults) {
68
                        if (vaults.hasOwnProperty('error')) {
69
                            var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]);
70
                            $scope.errors.push(errors);
71
72
                            callback(false);
73
                        }
74
                        else {
75
                            $scope.vaults = vaults;
76
                            callback(true);
77
                        }
78
                        $scope.$apply();
79
                    });
80
                },
81
                vault: function (callback) {
82
                    try {
83
                        PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password);
84
                        callback(true);
85
                    }
86
                    catch (e) {
87
                        $scope.errors.push(API.i18n.getMessage('invalid_vault_password'));
88
                        callback(false);
89
                    }
90
                },
91
                master: function (callback) {
92
                    if ($scope.settings.master_password.trim() !== '') {
93
                        callback(true);
94
                    } else {
95
                        $scope.errors.push(API.i18n.getMessage('empty_master_key'));
96
                        callback(false);
97
                    }
98
                }
99
            };
100
            $scope.saving = false;
101
            $scope.next = function () {
102
                $scope.saving = true;
103
                $scope.errors = [];
104
                $timeout(function () {
105
                    var step = StepsService.getCurrent().name;
106
                    var check = $scope.check[step];
107
                    if (typeof check === "function") {
108
                        check(function (result) {
109
                            $scope.saving = false;
110
                            if (result) {
111
                                $scope.errors = [];
112
                                $scope.$apply();
113
                                StepsService.steps().next();
114
                            }
115
                            $timeout(function () {
116
                                $scope.errors = [];
117
                                $scope.$apply();
118
                            }, 5000);
119
                        });
120
                    }
121
                    else {
122
                        $scope.saving = false;
123
                        StepsService.steps().next();
124
                    }
125
                }, 10);
126
            };
127
128
            $scope.finished = function () {
129
                var settings = angular.copy($scope.settings);
130
                var master_password = settings.master_password;
131
                var master_password_remember = settings.master_password_remember;
132
                delete settings.master_password;
133
                delete settings.master_password_remember;
134
                $scope.saving = true;
135
                API.runtime.sendMessage(API.runtime.id, {
136
                    method: "setMasterPassword",
137
                    args: {password: master_password, savePassword: master_password_remember}
138
                })
139
                    .then(function () {
140
                        API.runtime.sendMessage(API.runtime.id, {
141
                            method: "saveSettings",
142
                            args: settings
143
                        }).then(function () {
144
                            setTimeout(function () {
145
                                window.location = '#!/';
146
                                $scope.saving = false;
147
                            }, 1500);
148
                        });
149
                    });
150
151
152
            };
153
        }]);
154
}());
155
156